package cn.iocoder.yudao.module.system.controller.admin.tenant;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.product.TenantProductPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.product.TenantProductRespVO;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.product.TenantProductSaveReqVO;
import cn.iocoder.yudao.module.system.convert.tenant.TenantProductConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantProductDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.tenant.TenantProductService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 产品套餐")
@RestController
@RequestMapping("/system/tenant-product")
@Validated
public class TenantProductController {

    @Resource
    private TenantProductService tenantProductService;

    @Resource
    private AdminUserService adminUserService;

    @GetMapping("/page")
    @Operation(summary = "获得系统产品分页")
    public CommonResult<PageResult<TenantProductRespVO>> getTenantProductPage(@Valid TenantProductPageReqVO pageReqVO) {
        PageResult<TenantProductDO> pageResult = tenantProductService.getTenantProductPage(pageReqVO);
        Map<Long, AdminUserDO> userMap = adminUserService.getUserMap(pageResult.getList().stream().map(TenantProductDO::getOwnerUserId).collect(Collectors.toList()));
        return success(TenantProductConvert.INSTANCE.convert(pageResult, userMap));
    }

    @GetMapping({"/page-by-system", "/page-by-admin"})
    @Operation(summary = "获得系统产品分页")
    @TenantIgnore
    @DataPermission(enable = false)
    public CommonResult<PageResult<TenantProductRespVO>> getTenantProductPageBySystem(@Valid TenantProductPageReqVO pageReqVO) {
        return TenantUtils.execute(pageReqVO.getTenantId(), () -> getTenantProductPage(pageReqVO));
    }

}